import Vue from 'vue'
import Router from 'vue-router'
// 引入多个模块的规则
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionsRouter from './modules/permissions'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingsRouter from './modules/settings'
import socialsRouter from './modules/socials'


Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * 注意：二级菜单仅仅出现在子级路由个数大于等于1个的情况下
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   如果为true，则不会显示在侧边栏菜单，默认为 false
 * alwaysShow: true               如果设置为true，他将一直显示
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           重定向地址
 * name:'router-name'             路由的名称，必须要 <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    角色控制
    title: 'title'               标题
    icon: 'svg-name'/'el-icon-x' 图标
    breadcrumb: false            是否显示在面包屑中
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */


/**
 * 静态路由 是所有人都有的路由
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [{
            path: '/login',
            component: () =>
                import ('@/views/login/index'),
            hidden: true
        },
        {
            path: '/',
            component: Layout,
            redirect: '/dashboard',
            children: [{
                path: 'dashboard',
                name: 'Dashboard',
                component: () =>
                    import ('@/views/dashboard/index'),
                meta: {
                    title: 'Dashboard',
                    icon: 'dashboard'
                }
            }]
        },
        {
            path: '/404',
            component: () =>
                import ('@/views/404'),
            hidden: true
        },
        // 404 page must be placed at the end !!!
        {
            path: '*',
            redirect: '/404',
            hidden: true
        }
    ]
    // 动态路由
export const asyncRoutes = [
    approvalsRouter,
    departmentsRouter,
    employeesRouter,
    permissionsRouter,
    attendancesRouter,
    salarysRouter,
    settingsRouter,
    socialsRouter
]

const createRouter = () => new Router({
    // mode: 'history', // require service support
    // 管理滚动行为 如果出现滚动 切换就让 让页面回到顶部
    scrollBehavior: () => ({
        y: 0
    }),
    routes: [...constantRoutes, ...asyncRoutes] //临时合并所有的路由
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}

export default router